<?php

namespace App\Http\Controllers\Api\V4;

use App\Http\Controllers\Api\V4\BaseController;
use Illuminate\Http\Request;


use App\Models\V2\MemberModel;
use App\Models\V2\ReadPartyModel;
use App\Models\V2\CompanyModel;

use App\Common\ApiData;
use App\Repository\Comment;


class CompanyController extends BaseController
{

    use Comment;

    /**
     * 添加部门
     * */
    public function add_department(Request $request)
    {
        //1.检查参数
        $mobile         = $request->input('mobile');
        $key            = $request->input('key');
        $companyId      = $request->input('companyId');
        $departmentName = $request->input('departmentName');
        $readPartyId    = $request->input('readPartyId');
        $apiData        = new ApiData();

        $params      = [
            'mobile'    => [$mobile, "缺少手机号", "3001"],
            'key'       => [$key, "缺少设备ID", "3002"],
            'companyId' => [$companyId, "缺少公司ID", '3003'],
            'deName'    => [$departmentName, "缺少部门名称", "3004"],
            'readId'    => [$readPartyId,'缺少读书会ID，请升级APP','3005']
        ];
        $checkResult = $this->check_params($params);
        if ($checkResult->status == false) {
            $apiData->message = $checkResult->message;
            $apiData->code    = $checkResult->code;
            return $apiData->send();
        }

        $memberModel = new MemberModel();
        $memberInfo  = $memberModel->get_member_info_by_mobile_key($mobile, $key);
        if ($memberInfo == false) {
            $apiData->message = "用户不存在";
            $apiData->code    = "2001";
            return $apiData->send();
        }

        $data         = [
            'company_id'        => $companyId,
            'department_name'   => $departmentName,
            'read_party_id'     => $readPartyId
        ];

        $companyModel = new CompanyModel();
        $res          = $companyModel->update_department($data);
        if ($res) {
            $apiData->message = "添加成功";
            $apiData->code    = "200";
        } else {
            $apiData->message = "添加失败";
            $apiData->code    = "402";
        }
        return $apiData->send();
    }


    /*
     * 获得部门列表
     * */
    public function get_department(Request $request)
    {
        $mobile    = $request->input('mobile');
        $key       = $request->input('key');
        $companyId = $request->input('companyId');
        $readId    = $request->input('readPartyId',0);

        $companyModel = new CompanyModel();
        $departments  = $companyModel->get_company_department($companyId,$readId);
        return apiData()->set_data('departments',$departments)->send('200','ok');
    }


    /**
     * 获得公司架构信息
     * */
    public function get_company_orgization(Request $request)
    {
        $mobile      = $request->input('mobile');
        $key         = $request->input('key');
        $readPartyId = $request->input('readPartyId');
        $apiData     = new ApiData();

        $memberModel = new MemberModel();
        $memberInfo  = $memberModel->get_member_info_by_mobile_key($mobile, $key);
        if ($memberInfo == false) {
            $apiData->message = "用户不存在";
            $apiData->code    = "2001";
            return $apiData->send();
        }

        $readPartyModel = new ReadPartyModel();
        $readPartyInfo  = $readPartyModel->get_read_party_info($readPartyId);
        if ($readPartyInfo == false) {
            $apiData->message = "读书会不存在";
            $apiData->code    = "3000";
            return $apiData->send();
        }

        $companyId    = $readPartyInfo->company_id;
        $companyModel = new CompanyModel();
        $companyInfo  = $companyModel->get_company_info($companyId);

        if (!$companyInfo) {
            $apiData->message = "读书会不存在";
            $apiData->code    = "3001";
            return $apiData->send();
        }
        $departments = $companyModel->get_company_department($companyId,$readPartyId);
        foreach ($departments as $key => $depart) {
            $members             = $readPartyModel->get_read_party_department_member($readPartyId, $depart->department_id);
            $depart->members     = $members;
            $depart->totalMember = count($members);
        }
        $apiData->message = "ok";
        $apiData->code    = "200";
        return $apiData->set_data('companyInfo', $companyInfo)
            ->set_data('departments', $departments)
            ->send();
    }

    /**
     * 自课堂首页
     * */
    public function company_source(Request $request)
    {
        $readpartyId    = $request->input('readPartyId');
        $readPartyModel = new ReadPartyModel();
        $companyModel   = new CompanyModel();
        $readpartyInfo  = $readPartyModel->get_read_party_info($readpartyId);

        $companyId      = $readpartyInfo->company_id;
        $sourceType     = $companyModel->get_company_source_type($companyId,$readpartyId);
        $sourceType     = $sourceType->toArray();
        $all            = ['id'=>0,'type_name'=>'全部'];
        array_push($sourceType,$all);
        $companySource  = $companyModel->get_company_source($companyId,0,$readpartyId);
        foreach($companySource as $source)
        {
            if($source->category == 'article')
                $source->content         = env('HOST')."/home/companySourceDetail?sourceId=".$source->id;
        }
        return apiData()
            ->set_data('sourceTypes',$sourceType)
            ->set_data('sources',$companySource)
            ->send(200,'ok');
    }


    /*
     * 自课堂列表
     * */
    public function page_company_source(Request $request)
    {
        $readPartyId    = $request->input('readPartyId',0);
        $typeId         = $request->input('typeId',0);
        $readPartyModel = new ReadPartyModel();
        $companyModel   = new CompanyModel();

        $readPartyInfo  = $readPartyModel->get_read_party_info($readPartyId);
        $companyId      = $readPartyInfo->company_id;

        $companySource  = $companyModel->get_company_source($companyId,$typeId);
        foreach($companySource as $source)
        {
            if($source->category == 'article')
            $source->content         = env('HOST')."/home/companySourceDetail?sourceId=".$source->id;
        }
        return apiData()
            ->set_data('data',$companySource)
            ->send_old(200,'ok');
    }



    /**
     * 获得公司资源详情
     * */
    public function detail_company_source(Request $request)
    {

        $sourceId       = $request->input('sourceId',0);
        $mobile         = $request->input('mobile');
        $key            = $request->input('key');
        if($sourceId == 0)
        {
            return apiData()->send(3001,'缺少资源ID');
        }
        $memberModel    = new MemberModel();
        $memberInfo     = $memberModel->get_member_info_by_mobile_key($mobile,$key);

        $companyModel   = new CompanyModel();
        $sourceDetail   = $companyModel->get_source_detail($sourceId);

        if($sourceDetail->category == 'article')
        {
            $sourceDetail->content = env('HOST')."/home/companySourceDetail?sourceId=".$sourceDetail->id;
        }

        $comments = $this->get_multy_grade_comments($sourceId,4,0,$memberInfo->UserID);

        return apiData()
            ->set_data('source',$sourceDetail)
            ->set_data('comments',$comments)
            ->send(200,'ok');
    }



}